Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
phaser-tiled
Advanced tools
This is a replacement for the tilemap implementation in the Phaser game framework. The purpose of this plugin is to optimize the tilemaps for large complex maps built with the Tiled Map Editor.
This plugin optimizes the rendering of large scrolling tilemaps. It also adds support for many more Tiled features and lots of advanced functionality. You can read Why use this plugin? below for more details
Simply download the dist/phaser-tiled.js
script and include it on your page after including Phaser:
<script src="phaser.js"></script>
<script src="phaser-tiled.js"></script>
After adding the script to the page you can activate it by enabling the plugin:
game.add.plugin(Phaser.Plugin.Tiled);
Then you can add a Tiled map to your game like this:
// By using the built-in cache key creator, the plugin can
// automagically find all the necessary items in the cache
var cacheKey = Phaser.Plugin.Tiled.utils.cacheKey;
// load the tiled map, notice it is "tiledmap" and not "tilemap"
game.load.tiledmap(cacheKey('my-tiledmap', 'tiledmap'), 'assets/levels/tilemap.json', null, Phaser.Tilemap.TILED_JSON);
// load the images for your tilesets, make sure the last param to "cacheKey" is
// the name of the tileset in your map so the plugin can find it later
game.load.image(cacheKey('my-tiledmap', 'tileset', 'tileset1-name'), 'assets/levels/tileset1.png');
game.load.image(cacheKey('my-tiledmap', 'tileset', 'tileset2-name'), 'assets/levels/tileset2.png');
// if you have image layers, be sure to load those too! Again,
// make sure the last param is the name of your layer in the map.
game.load.image(cacheKey('my-tiledmap', 'layer', 'layer-name'), 'assets/levels/layer.png');
////////////
// Later after loading is complete:
// add the tiledmap to the game
// this method takes the key for the tiledmap which has been used in the cacheKey calls
// earlier, and an optional group to add the tilemap to (defaults to game.world).
var map = game.add.tiledmap('my-tiledmap');
That can get pretty heavy, and hardcoding what to load and how to name it can stink! Luckily, there is an easier way to handle it. Instead of hard-coding what the tilemap should load and be named, this plugin has a gulp task that can generate a Phaser Asset Pack that describes what and how to load the tiledmap. If you have this pack it becomes this simple to load and create a tiledmap:
// the key will be the filename of the map without the extension.
game.load.pack('my-tiledmap', 'assets/levels/tilemap-assets.json');
////////////
// Later after loading is complete:
var map = game.add.tiledmap('my-tiledmap');
Wow, that was a lot easier! You can find out more about the generator on it's GitHub page.
This plugin comes with a couple ways to implement physics for your games. Right now the only officially supported engine is p2.js, but hopefully arcade and others can join the party soon (need it now? submit a PR!).
To create the physics bodies based on a tilemap, the simplest way is to create an object layer in Tiled Editor and use the object tools to draw physics. You can use the rectangle, ellipse, polygon, or polyline tools. The only caveats are that circles (not ellipses) are supported so height and width of the ellipse must be the same, and if you use the polyline tool be sure to close the path to make a convex polygon!
Here is how you can convert your objects into collision bodies:
var map = game.add.tiledmap('tilemap-key');
game.physics.p2.convertTiledCollisionObjects(map, 'objectlayer-name');
That is it! All the objects in the layer named objectlayer-name
will be converted to p2 physics bodies and added
to the simulation.
The second method is to set the collides
custom property on tiles in a tileset to true
. This tells the engine that
that specific tile is collidable wherever it is in the map, and a body will be created for it.
Here is how you can convert your collidable tiles into collision bodies:
var map = game.add.tiledmap('tilemap-key');
game.physics.p2.convertTiledmap(map, 'tilelayer-name');
That is it! All the collidable tiles in the layer named tilelayer-name
will be converted to p2 physics bodies
and added to the simulation. This will also try to combine bodies that are adjacent on the same X axis into a single
body to reduce the total number of bodies that are created. This algorithm can (and should) be much improved.
Here are some features this plugin has that Phaser doesn't, or that this plugin tries to do better:
Using a large test map with 256x256 tiles, each 16x16 pixels, and 3 layers of them. phaser-debug gives me this performance graph for the core phaser tilemap implementation:
The spikes you see there are when I pan around the map. Using the same map with this plugin I get this:
None, yet.
None, yet.
collideLeft
- true will make this tile collide on the leftcollideRight
- true will make this tile collide on the rightcollideUp
- true will make this tile collide on the topcollideDown
- true will make this tile collide on the bottomcollides
- true will set all collision sides to true, if that collision side doesn't have a specific overrideblendMode
- string of the blendMode constant to use for this tile (e.g. 'NORMAL')batch
- true will place tile sprites into a SpriteBatch container.batch
- true will place object sprites into a SpriteBatch container.blendMode
- string of the blendMode constant to use for all objects in this layer (e.g. 'NORMAL').blendMode
- string of the blendMode constant to use for this object (e.g. 'NORMAL')None, yet.
Layer Properties:
Map Methods:
Object Layer:
Image Layer:
General:
FAQs
A tilemap implementation for phaser focusing on large complex maps
We found that phaser-tiled demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.